package com.xfcy.blog.controller.system;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xfcy.blog.annotation.OperationLogger;
import com.xfcy.blog.common.R;
import com.xfcy.blog.dto.CategoryDTO;
import com.xfcy.blog.service.CategoryService;
import com.xfcy.blog.valid.AddGroup;
import com.xfcy.blog.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 博客分类表(Category)表控制层
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:05
 */
@RestController
@RequestMapping("system/category")
@Api(tags = "博客分类管理")
public class CategoryController {

    @Resource
    private CategoryService categoryService;


    @GetMapping("/list")
    @ApiOperation(value = "后台分类列表", httpMethod = "GET", response = R.class, notes = "后台分类列表")
    @SaCheckLogin
    public R list(Integer pageNo, Integer pageSize, String name) {
        return categoryService.listRecord(pageNo, pageSize, name);
    }

    @OperationLogger(value = "分类删除")
    @DeleteMapping("/delete")
    @ApiOperation(value = "后台分类删除", httpMethod = "DELETE", response = R.class, notes = "后台分类删除")
    @SaCheckPermission("/system/category/delete")
    public R delete(Long id) {
        return categoryService.deleteCategoryId(id);
    }

    @OperationLogger(value = "分类批量删除")
    @DeleteMapping("/deleteBatch")
    @ApiOperation(value = "后台分类批量删除", httpMethod = "DELETE", response = R.class, notes = "后台分类批量删除")
    @SaCheckPermission("/system/category/deleteBatch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        return categoryService.deleteBatchCategoryId(ids);
    }

    @OperationLogger(value = "分类置顶或取消")
    @GetMapping("/top")
    @ApiOperation(value = "后台分类置顶或取消", httpMethod = "GET", response = R.class, notes = "后台分类置顶或取消")
    @SaCheckPermission("/system/category/top")
    public R top(Long id) {
        return categoryService.top(id);
    }

    @OperationLogger(value = "添加分类")
    @PostMapping("/add")
    @SaCheckPermission("/system/category/add")
    @ApiOperation(value = "添加分类", httpMethod = "POST", response = R.class, notes = "添加分类")
    public R add(@Validated(AddGroup.class) @RequestBody CategoryDTO categoryDTO) {
        return categoryService.addCategory(categoryDTO);
    }

    @OperationLogger(value = "分类信息")
    @GetMapping("/info")
    @SaCheckPermission("/system/category/info")
    @ApiOperation(value = "分类信息", httpMethod = "GET", response = R.class, notes = "分类信息")
    public R info(Long id) {
        return categoryService.getCategoryById(id);
    }



    @PostMapping("/update")
    @SaCheckPermission("/system/category/update")
    @ApiOperation(value = "修改分类", httpMethod = "POST", response = R.class, notes = "修改分类")
    public R update(@Validated(UpdateGroup.class) @RequestBody CategoryDTO categoryDTO) {
        return categoryService.updateCategory(categoryDTO);
    }


}

